package com.tc.multicast;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class NativeInputStream extends InputStream{

	Multicast core = null;
	PipedOutputStream out = new PipedOutputStream();
	PipedInputStream in = new PipedInputStream();
	public NativeInputStream(Multicast mc)
	{
		this.core = mc;

		try {
			in.connect(out);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Thread t = new Thread("multicast thread"){

			@Override
			public void run() {

				while(true){
				
				try {
					byte[] buf = core.receive();
					
					String s = new String(buf);
					System.err.println(s);
					if(buf.length <=5)
						continue;
					out.write(buf);
					out.flush();
					
				} catch (Throwable e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				}
				
			}
			
		};
		t.setPriority(Thread.MAX_PRIORITY);
		t.start();
		
		
	}
	@Override
	public int read() throws IOException {
		

		return in.read();

	}

	
	@Override
	public int read(byte[] bytes, int offset, int count) throws IOException {
		return in.read(bytes, offset, count);
		
	}
	@Override
	public int read(byte[] b) throws IOException {
		return in.read(b);
	}
	
}
